project(
  'devmon',
  'cpp',
  version: '0.1.0',
  default_options: [
    'warning_level=3',
    'cpp_std=c++17',
    'install_inc_dir=include',
    'cpp_args=-fPIC -Wall -Wno-unused-parameter -Wno-unused-variable -Wno-sign-compare -Wno-pedantic -Wno-deprecated-copy',
  ],
)

add_project_arguments('-Wall', '-Wextra', '-Wno-#warnings', '-fvisibility=hidden', language: 'cpp')
add_project_arguments(
  '-fpermissive',
  '-Wno-error=attributes',
  '-Wno-error=deprecated-declarations',
  language: 'cpp',
)

# 区分conan和meson构建
meson_build = get_option('meson_build')
# 是否为DT构建
build_tests = get_option('tests')

# 设置编译宏
if meson.is_cross_build()
  current_buildtype = get_option('buildtype')
  if current_buildtype == 'debug'
    add_project_arguments('-DBUILD_TYPE=BUILD_TYPE_DEBUG', language: 'cpp')
  else
    add_project_arguments('-DBUILD_TYPE=BUILD_TYPE_RELEASE', language: 'cpp')
  endif
else
  add_project_arguments('-DBUILD_TYPE=BUILD_TYPE_DT', language: 'cpp')
endif


# 定义驱动库路径
if build_tests
  # 测试环境驱动库路径（相对于构建目录）
  chip_drv_path = meson.current_build_dir() / 'test_drvlib/chip'
  bus_drv_path = meson.current_build_dir() / 'test_drvlib/bus'
  # 确保测试目录存在
  run_command('mkdir', '-p', chip_drv_path, check: false)
  run_command('mkdir', '-p', bus_drv_path, check: false)
else
  # 生产环境驱动库路径
  chip_drv_path = '/opt/bmc/apps/devmon/drvlib/chip'
  bus_drv_path = '/opt/bmc/apps/devmon/drvlib/bus'
endif

# 创建配置头文件
conf_data = configuration_data()
conf_data.set_quoted('DEVMON_CHIP_DRV_PATH', chip_drv_path)
conf_data.set_quoted('DEVMON_BUS_DRV_PATH', bus_drv_path)
configure_file(
  output : 'devmon_config.h',
  configuration : conf_data
)

is_darwin = host_machine.system() == 'darwin'

include_dirs = [
  include_directories('include'),
]

enable_conan_compile = get_option('enable_conan_compile')
add_project_arguments(
  '-DENABLE_CONAN_COMPILE=' + enable_conan_compile.to_int().to_string(),
  language: 'cpp',
)

if meson_build
  install_dir = meson.project_build_root() + '/'
  run_command('mkdir', '-p', install_dir, check: false)

  # 获取安装头文件目录，默认为构建目录下的include
  install_inc_dir = get_option('install_inc_dir')
  if install_inc_dir == 'include' # 默认值
    install_inc_dir = meson.project_build_root() / 'include'
  endif
  # 确保头文件安装目录存在
  run_command('mkdir', '-p', install_inc_dir, check: false)
else
  install_dir = ''
endif

if meson_build
  libmcpp_dep = dependency('libmcpp',
    fallback: ['libmcpp', 'libmcpp_dep'],
    default_options: ['tests=false'],
    required: true,
  )
else 
  libmcpp_dep = dependency('libmcpp', method: 'pkg-config', required: true)
endif

if meson_build
  internal_dep = dependency('internal',
    fallback: ['component_drivers', 'internal_dep'],
    default_options: ['tests=false'],
    required: true,
  )
else 
  internal_dep = dependency('Internal', method: 'pkg-config', required: true)
endif

dbus_dep = dependency('dbus-1', method: 'pkg-config', required: true)
glib_dep = dependency('glib-2.0', method: 'pkg-config', required: true)

# 尝试查找 lua 依赖（用于链接 libsoc_adapter）
lua_dep = dependency('luajit', method: 'pkg-config', required: false)

libmcpp_deps = [libmcpp_dep, dbus_dep, glib_dep]
if lua_dep.found()
  libmcpp_deps += [lua_dep]
endif

subdir('src')

# 安装dist/user_conf/rootfs/目录内容
install_subdir('dist/user_conf/rootfs/etc/systemd', install_dir: install_dir + 'etc')

# 安装config.json配置文件
install_data('config.json', install_dir: install_dir + 'opt/bmc/apps/devmon')

if meson_build and build_tests
  test_utilities_dep = dependency('test_utilities',
    fallback: ['libmcpp', 'test_utilities_dep'],
    default_options: [],
    required: true,
  )
  subdir('tests')
endif
